package date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @auther XuGeGe
 * @date 2022/7/7
 */
public class Reast {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        System.out.println("date: " + date);
        /*System.out.println("date: " + df.format(date));
        System.out.println("time: "+ date.getTime());
        Long reastTime = getReastTime(date);
        System.out.println("reastTime: " + reastTime);
        date.setTime(reastTime);
        System.out.println("date: " + df.format(date));
        System.out.println("--------------------------------------");



        Date reset = reset(date);
        System.out.println("reset : " + df.format(reset));*/

        /*System.out.println("--------------------------------");
        Date reastDate = getReastDate(date);
        System.out.println("reastDate Df: " + df.format(reastDate));
        System.out.println("reastDate " + reastDate);*/


        /*System.out.println("--------------------------------");
        Date date1 = df.parse("2022-03-02 10:00:00");
        Date date2 = df.parse("2022-03-04 12:00:00");
        int dayCount = getDayCount(date1, date2);
        System.out.println("dayCount :"+dayCount);*/


        /*System.out.println("--------------------------------");
        int a = -3;
        int abs = Math.abs(a);
        System.out.println("abs : " + abs);
        String s = abs + "天前";
        System.out.println(s);*/

//        System.out.println("--------------------------------");
//        Date date1 = new Date();
//        int hours = date1.getHours();
//        System.out.println(hours);
//
//        Date parse = df.parse("2022-12-2 01:00:00");
//        int hours1 = parse.getHours();
//        System.out.println(hours1);

//        Date reastDate = getReastDate(null);
//        System.out.println(reastDate);

    }

    //获取格式化后（只有年月日）的时间
    public static Long getReastTime(Date date) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateFirst = dateFormat.format(date);
        Date parse = dateFormat.parse(dateFirst);
        return parse.getTime();

    }

    //规格化日期时间为   00：00：00
    public static Date getReastDate(Date date) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateFirst = dateFormat.format(date);
        Date parse = dateFormat.parse(dateFirst);
        return parse;
    }


    //规格化日期时间为   00：00：00
    public static Date reset(Date date) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("date: " + sf.format(date));
        Calendar cal = Calendar.getInstance();       // get calendar instance
        cal.setTime(date);                           // set cal to date
        cal.set(Calendar.HOUR_OF_DAY, 0);            // set hour to midnight
        cal.set(Calendar.MINUTE, 0);                 // set minute in hour
        cal.set(Calendar.SECOND, 0);                 // set second in minute
        cal.set(Calendar.MILLISECOND, 0);            // set millis in second
        Date zeroedDate = cal.getTime();             // actually computes the new Date
        System.out.println("zeroedDate: " + sf.format(zeroedDate));
        return zeroedDate;
    }


    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     * 如果只是简单的使用毫秒相减，会有误差，所以先统一时分秒
     */
    public static int getDayCount(Date date1, Date date2) {
        date1 = CalToDate(date1);
        date2 = CalToDate(date2);
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    /**
     * 统一时分秒
     */
    public static Date CalToDate(Date date1) {
        Calendar date = Calendar.getInstance();
        date.setTime(date1);
        int year = date.get(Calendar.YEAR);
        int mouth = date.get(Calendar.MONTH);
        int day = date.get(Calendar.DATE);
        Calendar data2 = Calendar.getInstance();
        data2.set(year, mouth, day, 23, 59, 59);
        return data2.getTime();
    }
}
